package com.wen.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wen.common.R;
import com.wen.pojo.Supplier;
import com.wen.pojo.dto.SupplierDto;
import com.wen.pojo.dto.wen.SupplierANDProductDto;
import com.wen.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private SupplierService service;


    @PutMapping("")
    public R supUpdate(@RequestBody Supplier supplier){

        UpdateWrapper<Supplier> supplierUpdateWrapper = new UpdateWrapper<>();
        supplierUpdateWrapper.eq("supplier_id",supplier.getSupplierId());
        boolean flag = service.update(supplier,supplierUpdateWrapper);
        if(flag)
            return R.ok().message("修改成功");
        return R.error().message("修改失败");
    }

    @PostMapping("sel/{pageNum}/{pageSize}")
    public R sel(@RequestBody SupplierDto dto,@PathVariable Long pageNum,@PathVariable Long pageSize){
        Page<Supplier> supplierPage = service.queryByPage(dto, pageNum, pageSize);
        return R.ok().data("Supplier",supplierPage.getRecords()).
                data("total",supplierPage.getTotal()).
                data("current",supplierPage.getCurrent()).
                data("size",supplierPage.getSize());
    }

     @GetMapping("selById")
    public R selById(String id){
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("supplier_id", id);
        List<Supplier> supplier = service.listByMap(columnMap);
        return R.ok().data("selById",supplier);
    }

    @PostMapping("up")
    public R up(@RequestBody Supplier supplier){
        System.out.println(supplier);

        UpdateWrapper<Supplier> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("supplier_id",supplier.getSupplierId());
        boolean b = service.update(supplier,updateWrapper);
        if(b)
            return R.ok().message("修改成功");
        return R.ok().message("修改失败");
    }

  @PostMapping("selectBysuid/{id}")
  public R selectBysuid(@PathVariable String id){

      Supplier byId = service.getById(id);

      return R.ok().data("bean",byId);
  }

  @PostMapping("page2/{name}/{id}")
  public R page2(@PathVariable String name,@PathVariable String id){

      List<SupplierANDProductDto> sel = service.sel(name,id);

      return R.ok().data("files",sel);
  }


    /**
     * 供应商添加
     * @param supplier
     * @return
     */
    @PostMapping("/SupplierAdd")
    public R addProduct(@RequestBody Supplier supplier){
        boolean flag = service.save(supplier);
        if (flag) {
            return R.ok().message("添加成功！");
        }
        return R.error().message("添加失败！");
    }
}
